


// Author:					HungNM
// Created:					16-08-2012
// Last Modified:			16-08-2012
//
	
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using eProject.Data;
	
namespace eProject.Business
{
	
	public class AirPorts
	{

		#region Constructors

	    public AirPorts()
		{}
	    
	
	    public AirPorts(
			long id) 
		{
	        GetAirPorts(
				id); 
	    }

		#endregion

		#region Private Properties

		private long id;
		private string airPortId = string.Empty;
		private string name = string.Empty;
		private int cityId = -1;
		private bool isDeleted = false;
		private DateTime createdDate = DateTime.UtcNow;
		private DateTime lastUpdate = DateTime.UtcNow;
		
		#endregion

		#region Public Properties

		public long Id 
		{
			get { return id; }
			set { id = value; }
		}
		public string AirPortId 
		{
			get { return airPortId; }
			set { airPortId = value; }
		}
		public string Name 
		{
			get { return name; }
			set { name = value; }
		}
		public int CityId 
		{
			get { return cityId; }
			set { cityId = value; }
		}
		public bool IsDeleted 
		{
			get { return isDeleted; }
			set { isDeleted = value; }
		}
		public DateTime CreatedDate 
		{
			get { return createdDate; }
			set { createdDate = value; }
		}
		public DateTime LastUpdate 
		{
			get { return lastUpdate; }
			set { lastUpdate = value; }
		}

		#endregion

		#region Private Methods

		/// <summary>
		/// Gets an instance of AirPorts.
		/// </summary>
		/// <param name="id"> id </param>
		private void GetAirPorts(
			long id) 
		{
			try
			{
				using(IDataReader reader = DBAirPorts.GetOne(
					id)) 
				{
					PopulateFromReader(reader);
				}
			}
			catch(Exception ex)
			{
				throw ex;
			}
		}
		
		
		private void PopulateFromReader(IDataReader reader) 
		{
			try
			{
				if(reader.Read())
				{
					this.id = Convert.ToInt64(reader["Id"]);
					this.airPortId = reader["AirPortId"].ToString();
					this.name = reader["Name"].ToString();
					this.cityId = Convert.ToInt32(reader["CityId"]);
					this.isDeleted = Convert.ToBoolean(reader["IsDeleted"]);
					this.createdDate = Convert.ToDateTime(reader["CreatedDate"]);
					this.lastUpdate = Convert.ToDateTime(reader["LastUpdate"]);
				
				}
			}
			catch(Exception ex)
			{
				throw ex;
			}
		}
		
		/// <summary>
        /// Persists a new instance of AirPorts. Returns true on success.
        /// </summary>
        /// <returns></returns>
		private bool Create()
		{ 
			int newID = 0;
			
			try
			{
				newID = DBAirPorts.Create(
					this.airPortId, 
					this.name, 
					this.cityId, 
					this.isDeleted, 
					this.createdDate, 
					this.lastUpdate); 
					
				this.id = newID;
			}
			catch(Exception ex)
			{
				throw ex;
			}
			
			return (newID > 0);
		}

		
		/// <summary>
        /// Updates this instance of AirPorts. Returns true on success.
        /// </summary>
        /// <returns>bool</returns>
		private bool Update()
		{
			bool IsUpdated = false;
			try
			{
				IsUpdated = DBAirPorts.Update(
					this.id, 
					this.airPortId, 
					this.name, 
					this.cityId, 
					this.isDeleted, 
					this.createdDate, 
					this.lastUpdate); 
			}
			catch(Exception ex)
			{
				throw ex;
			}
			
			return IsUpdated;
		}

		#endregion

		#region Public Methods

		/// <summary>
        /// Saves this instance of AirPorts. Returns true on success.
        /// </summary>
        /// <returns>bool</returns>
		public bool Save()
		{
			bool IsSaved = false;
			try
			{
				if(this.id > 0)
				{
					IsSaved = Update();
				}
				else
				{
					IsSaved = Create();
				}
			}
			catch(Exception ex)
			{
				throw ex;
			}
			
			return IsSaved;
		}
		
		#endregion

		#region Static Methods

		/// <summary>
		/// Deletes an instance of AirPorts. Returns true on success.
		/// </summary>
		/// <param name="id"> id </param>
		/// <returns>bool</returns>
		public static bool Delete(
			long  id) 
		{
			bool IsDeleted = false;
			try
			{
				IsDeleted = DBAirPorts.Delete(
					id); 
			}
			catch(Exception ex)
			{
				throw ex;
			}
			
			return IsDeleted;
		}

        public static List<AirPorts> getSearch(string text, bool delete) {
            List<AirPorts> listAirPorts = new List<AirPorts>();
            try
            {
                IDataReader reader = DBAirPorts.getSearch(text,delete);
                listAirPorts = LoadListFromReader(reader);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return listAirPorts;
        }

		/// <summary>
		/// Gets a count of AirPorts. 
		/// </summary>
		public static int GetCount()
		{
			int count = 0;
			try
			{
				count = DBAirPorts.GetCount();
			}
			catch(Exception ex)
			{
				throw ex;
			}
			
			return count;
		}
	
		private static List<AirPorts> LoadListFromReader(IDataReader reader)
		{
			List<AirPorts> airPortsList = new List<AirPorts>();
			try
			{
				while (reader.Read())
				{
					AirPorts airPorts = new AirPorts();
					airPorts.id = Convert.ToInt64(reader["Id"]);
					airPorts.airPortId = reader["AirPortId"].ToString();
					airPorts.name = reader["Name"].ToString();
					airPorts.cityId = Convert.ToInt32(reader["CityId"]);
					airPorts.isDeleted = Convert.ToBoolean(reader["IsDeleted"]);
					airPorts.createdDate = Convert.ToDateTime(reader["CreatedDate"]);
					airPorts.lastUpdate = Convert.ToDateTime(reader["LastUpdate"]);
					airPortsList.Add(airPorts);
					
				}
			}
			finally
			{
				reader.Close();
			}
	
			return airPortsList;
		}

        /// <summary>
        /// Gets an IList with all instances of Cities.
        /// </summary>
        public static List<AirPorts> GetAllByRoute(bool isFrom)
        {
            List<AirPorts> listCities = new List<AirPorts>();
            try
            {
                IDataReader reader = DBAirPorts.GetAllByRoute(isFrom);
                listCities = LoadListFromReader(reader);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return listCities;
        }

		/// <summary>
		/// Gets an IList with all instances of AirPorts.
		/// </summary>
		public static List<AirPorts> GetAll()
		{
			List<AirPorts> listAirPorts = new List<AirPorts>();
			try
			{
				IDataReader reader = DBAirPorts.GetAll();
				listAirPorts = LoadListFromReader(reader);
			}
			catch(Exception ex)
			{
				throw ex;
			}
			
			return listAirPorts;
		}
	
		/// <summary>
		/// Gets an IList with page of instances of AirPorts.
		/// </summary>
		/// <param name="pageNumber">The page number.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="totalPages">total pages</param>
		public static List<AirPorts> GetPage(int pageNumber, int pageSize, out int totalPages)
		{
			totalPages = 1;
			List<AirPorts> listAirPorts = new List<AirPorts>();
			try
			{
				IDataReader reader = DBAirPorts.GetPage(pageNumber, pageSize, out totalPages);	
				listAirPorts = LoadListFromReader(reader);
			}
			catch(Exception ex)
			{
				throw ex;
			}
			
			return listAirPorts;
		}
		
		#endregion

	}
}





